from flask import Flask
from flask_login import LoginManager
from app.models.base import db
from flask_mail import Mail

login_manager = LoginManager()
mail = Mail()

def create_app():
    """
    创建app对象
    :return: app
    """
    app = Flask(__name__)
    # 载入配置
    app.config.from_object('app.secure')
    app.config.from_object('app.setting')
    register_blueprint(app)
    #

    db.init_app(app)
    login_manager.init_app(app)
    # 告诉flask-login那个是登录页面，如果是前后端分离这里用不上
    login_manager.login_view = 'web.login'
    login_manager.login_message = '请先登录或注册'

    mail.init_app(app)

    with app.app_context():
        db.create_all()
    return app


def register_blueprint(app):
    """
    把所有的蓝图注册到app实例上
    :param app:
    """
    from app.web.book import web
    app.register_blueprint(web)
